package constant.milk.cashbook.asset.credit;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;

import constant.milk.cashbook.R;
import constant.milk.cashbook.SuperActivity;
import constant.milk.cashbook.asset.AddFlagActivity;
import constant.milk.cashbook.asset.CountActivity;
import constant.milk.cashbook.asset.LinkIdBankActivity;
import constant.milk.cashbook.asset.MemoActivity;
import constant.milk.cashbook.asset.MoneyActivity;
import constant.milk.cashbook.asset.NameActivity;
import constant.milk.cashbook.bean.BankData;
import constant.milk.cashbook.database.DBSelectQuery;
import constant.milk.cashbook.database.DbInsertQuery;
import constant.milk.cashbook.tip.TipInfoActivity;
import constant.milk.cashbook.tip.TipMap;
import constant.milk.cashbook.util.MilkManager;
import constant.milk.cashbook.util.MilkUtil;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class CreditInsertActivity extends SuperActivity
{
	private static final int NAME_REQUEST_CODE = 0;
	private static final int MONEY_REQUEST_CODE = 1;
	private static final int COUNT_REQUEST_CODE = 2;
	private static final int MEMO_REQUEST_CODE = 3;
	private static final int LINKID_REQUEST_CODE = 4;
	private static final int AUTOYN_REQUEST_CODE = 5;
	private static final int TERMDATE_REQUEST_CODE = 6;
	private static final int RESULTDATE_REQUEST_CODE = 7;
	
	private View nameView;
	private View moneyView;
	private View countView;
	private View memoView;
	private View linkidView;
	private View autoynView;
	private View termdateView;
	private View resultdateView;
	
	private TextView nameTextView;
	private TextView moneyTextView;
	private TextView countTextView;
	private TextView memoTextView;
	private TextView linkidTextView;
	private TextView autoynTextView;
	private TextView termdateTextView1;
	private TextView termdateTextView2;
	private TextView resultdateTextView;
	
	private Button saveButton;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.creditcard_insert_activity);
		
		setTitle("신용카드추가");
		
		setFindViewId();
		setOnClick();
		setData();
	}

	@Override
	protected void setFindViewId()
	{
		nameView = findViewById(R.id.creditInsertNameView);
		moneyView = findViewById(R.id.creditInsertMoneyView);
		countView = findViewById(R.id.creditInsertCountView);
		memoView = findViewById(R.id.creditInsertMemoView);
		linkidView = findViewById(R.id.creditInsertLinkidView);
		autoynView = findViewById(R.id.creditInsertAutoynView);
		termdateView = findViewById(R.id.creditInsertTermdateView);
		resultdateView = findViewById(R.id.creditInsertResultdateView);
		
		nameTextView = (TextView) findViewById(R.id.creditInsertNameTextView);
		moneyTextView = (TextView) findViewById(R.id.creditInsertMoneyTextView);
		countTextView = (TextView) findViewById(R.id.creditInsertCountTextView);
		memoTextView = (TextView) findViewById(R.id.creditInsertMemoTextView);
		linkidTextView = (TextView) findViewById(R.id.creditInsertLinkidTextView);
		autoynTextView = (TextView) findViewById(R.id.creditInsertAutoynTextView);
		termdateTextView1 = (TextView) findViewById(R.id.creditInsertTermdate1TextView);
		termdateTextView2 = (TextView) findViewById(R.id.creditInsertTermdate2TextView);
		resultdateTextView = (TextView) findViewById(R.id.creditInsertResultdateTextView);
		
		saveButton = (Button) findViewById(R.id.saveButton);
	}

	@Override
	protected void setOnClick()
	{
		nameView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(superContext, NameActivity.class);
				intent.putExtra(NameActivity.NAME_DATA, nameTextView.getText().toString());
				startActivityForResult(intent, NAME_REQUEST_CODE);
			}
		});
		
		moneyView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(superContext, MoneyActivity.class);
				intent.putExtra(MoneyActivity.MONEY_DATA, moneyTextView.getText().toString());
				startActivityForResult(intent, MONEY_REQUEST_CODE);
			}
		});
		
		countView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(superContext, CountActivity.class);
				intent.putExtra(CountActivity.COUNT_DATA, countTextView.getText().toString());
				startActivityForResult(intent, COUNT_REQUEST_CODE);
			}
		});
		
		memoView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(superContext, MemoActivity.class);
				intent.putExtra(MemoActivity.MEMO_DATA, memoTextView.getText().toString());
				startActivityForResult(intent, MEMO_REQUEST_CODE);
			}
		});
		
		linkidView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(superContext, LinkIdBankActivity.class);
				intent.putExtra(LinkIdBankActivity.LINK_ASSET, getString(R.string.bankAsset));
				intent.putExtra(LinkIdBankActivity.LINK_DATA, linkidTextView.getText().toString());
				startActivityForResult(intent, LINKID_REQUEST_CODE);
			}
		});
		
		autoynView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(superContext, AutoynActivity.class);
				intent.putExtra(AutoynActivity.AUTOYN_DATA, autoynTextView.getText().toString());
				startActivityForResult(intent, AUTOYN_REQUEST_CODE);
			}
		});
		
		termdateView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(superContext, TermdateActivity.class);
				intent.putExtra(TermdateActivity.TERMDATE_DATA, termdateTextView1.getText().toString());
				startActivityForResult(intent, TERMDATE_REQUEST_CODE);
			}
		});
		
		resultdateView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(superContext, ResultdateActivity.class);
				intent.putExtra(ResultdateActivity.RESULTDATE_DATA, resultdateTextView.getText().toString());
				startActivityForResult(intent, RESULTDATE_REQUEST_CODE);
			}
		});
		
		saveButton.setOnClickListener(new OnClickListener()
		{
			
			public void onClick(View v)
			{
				if (setCheckData())
				{
					return;
				}
				
				if (setSaveData())
				{
					MilkUtil.showDialogFinish(superContext, "확인", "데이터 저장에 성공하였습니다.");
				}
				else
				{
					MilkUtil.showDialog(superContext, "실패", "잠시후 다시해 주세요.");
				}
			}
		});
	}

	@Override
	protected void setData()
	{
		countTextView.setText("0");
		
		DBSelectQuery db = new DBSelectQuery(superContext, superDb);
		
		ArrayList<BankData> list = db.getBankList();
		
		if (list.size() < 1)
		{
			MilkUtil.showToast(superContext, "통장이 하나도 없습니다. 통장부터 만들어 주세요.");
			finish();
		}
		else
		{
			BankData data = list.get(0);
			linkidTextView.setText(data.getName());	
			moneyTextView.setText("0");
		}
		
		autoynTextView.setText(R.string.autoYes);
		
		termdateTextView1.setText("1");
		termdateTextView2.setText("31");
		
		resultdateTextView.setText("3");
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		super.onActivityResult(requestCode, resultCode, data);
		
		if (resultCode != Activity.RESULT_OK)
		{
			return;
		}
		
		if (requestCode == NAME_REQUEST_CODE)
		{
			String str = data.getStringExtra(NameActivity.NAME_DATA);
			nameTextView.setText(str);
		}
		else if (requestCode == MONEY_REQUEST_CODE)
		{
			String str = data.getStringExtra(MoneyActivity.MONEY_DATA);
			moneyTextView.setText(str);
		}
		else if (requestCode == COUNT_REQUEST_CODE)
		{
			String str = data.getStringExtra(CountActivity.COUNT_DATA);
			countTextView.setText(str);
		}
		else if (requestCode == MEMO_REQUEST_CODE)
		{
			String str = data.getStringExtra(MemoActivity.MEMO_DATA);
			memoTextView.setText(str);
		}
		else if (requestCode == LINKID_REQUEST_CODE)
		{
			String str = data.getStringExtra(LinkIdBankActivity.LINK_DATA);
			linkidTextView.setText(str);
		}
		else if (requestCode == AUTOYN_REQUEST_CODE)
		{
			String str = data.getStringExtra(AutoynActivity.AUTOYN_DATA);
			autoynTextView.setText(str);
		}
		else if (requestCode == TERMDATE_REQUEST_CODE)
		{
			String str = data.getStringExtra(TermdateActivity.TERMDATE_DATA);
			termdateTextView1.setText(str);
			
			int day = Integer.parseInt(str);
			
			if (day == 1)
			{
				day = 31;
			}
			else
			{
				day = day - 1;
			}
			
			termdateTextView2.setText(day + "");
		}
		else if (requestCode == RESULTDATE_REQUEST_CODE)
		{
			String str = data.getStringExtra(ResultdateActivity.RESULTDATE_DATA);
			resultdateTextView.setText(str);
		}
	}
	
	/**
	 * 테이터가 제대로 있는지 확인한다.
	 */
	private boolean setCheckData()
	{
		String name = nameTextView.getText().toString();
		
		if (name.equals("") || name.length() < 1)
		{
			MilkUtil.showDialog(superContext, "확인", "이름을 입력해주세요.");
			return true;
		}
		
		DBSelectQuery query = new DBSelectQuery(superContext, superDb);
		if (query.getCreditCount(name) > 0)
		{
			MilkUtil.showDialog(superContext, "확인", "같은 이름의 카드가 있습니다.");
			nameTextView.setText("");
			return true;
		}
		
		String money = MilkUtil.getCommaDel(moneyTextView.getText().toString());
		
		if (money.equals("") || money.length() < 1)
		{
			MilkUtil.showDialog(superContext, "확인", "금액을 입력해주세요.");
			return true;
		}
		
		return false;
	}
	
	private boolean setSaveData()
	{
		DbInsertQuery query = null;
		
		try
		{
			query = new DbInsertQuery(superContext, superDb);
			
			query.setBeginTansaction();
			
			HashMap<String, String> map = new HashMap<String, String>();
			map.put("name", nameTextView.getText().toString());
			map.put("money", MilkUtil.getCommaDel(moneyTextView.getText().toString()));
			
			map.put("count", countTextView.getText().toString());
			
			map.put("memo", memoTextView.getText().toString());
			
			DBSelectQuery selectQuery = new DBSelectQuery(superContext, superDb);
			BankData data = selectQuery.getBankName(linkidTextView.getText().toString());
			map.put("linkid", data.getId());
			
			if (autoynTextView.getText().toString().equals(getResources().getString(R.string.autoNo)))
			{
				map.put("autoyn", "1");
			}
			else
			{
				map.put("autoyn", "0");
			}
			
			map.put("termdate", termdateTextView1.getText().toString());
			map.put("resultdate", resultdateTextView.getText().toString());
			
			MilkManager milkManager = new MilkManager(superContext, superDb);
			
			map.put("signdate", milkManager.getMonth(resultdateTextView.getText().toString()));
			
			query.setCreditCardInsert(map);
			
			query.setSuccessful();
		} catch (Exception e)
		{
			e.printStackTrace();
			return false;
		} finally {
			try
			{
				query.setEndTansaction();
			} catch (Exception e)
			{
				e.printStackTrace();
				return false;
			}
		}
		
		return true;
	}

	@Override
	protected void tipButton()
	{
		Intent intent = new Intent(superContext, TipInfoActivity.class);
		intent.putExtra(TipInfoActivity.TIP_ID, TipMap.TIP_ASSET_CREDITCARD_INSERT);
		startActivity(intent);
	}

}
